package com.atguigu.flink.sql;

import com.atguigu.flink.function.WaterSensorMapFunction;
import com.atguigu.flink.pojo.WaterSensor;
import org.apache.flink.api.common.eventtime.WatermarkStrategy;
import org.apache.flink.configuration.Configuration;
import org.apache.flink.streaming.api.datastream.SingleOutputStreamOperator;
import org.apache.flink.streaming.api.environment.StreamExecutionEnvironment;
import org.apache.flink.table.api.Table;
import org.apache.flink.table.api.bridge.java.StreamTableEnvironment;

import static org.apache.flink.table.api.Expressions.$;

/**
 * Created by Smexy on 2022/12/21
 */
public class Demo10_GroupSlideWindow
{
    public static void main(String[] args) {


        Configuration conf = new Configuration();
        conf.setInteger("rest.port", 3333);
        StreamExecutionEnvironment env = StreamExecutionEnvironment.getExecutionEnvironment(conf);
        StreamTableEnvironment tableEnv = StreamTableEnvironment.create(env);

        env.setParallelism(1);

        String  createTableSQL = " CREATE TABLE ws( id string, a bigint , vc int , " +
            "                              pt as proctime()  ," +
            "                              et  AS TO_TIMESTAMP_LTZ(a, 3) ," +
            "                              WATERMARK FOR et AS et - INTERVAL '0.001' SECONDS   ) " +
            "                       WITH (  " +
            "                         'connector' = 'filesystem',  " +
            "                         'path' = 'data/sensor.txt',   " +
            "                         'format' = 'csv'    " +
            "                            )      ";

        tableEnv.executeSql(createTableSQL);
/*
        tableEnv.sqlQuery("select * from ws ")
                .execute()
                .print();*/



        String slideSql2 = "select  id," +
            "              HOP_start(et, INTERVAL '3' SECOND,INTERVAL '5' SECOND)   wstart, " +
            "             HOP_end(et, INTERVAL '3' SECOND,INTERVAL '5' SECOND)  wend, " +
            "               sum(vc) sumVc   " +
            "        from ws " +
            "        group by id, HOP(et, INTERVAL '3' SECOND,INTERVAL '5' SECOND)  ";


        tableEnv.executeSql(slideSql2)
                 .print();

    }
}
